package org.com.ashen.service.datasourceswitch;

import org.com.ashen.common.exception.BusinessException;
import org.com.ashen.common.exception.ErrorEnum;
import org.com.ashen.dal.dataobject.SaasAppSession;
import org.com.ashen.dal.dataobject.SaasSession;
import org.com.ashen.service.config.JdbcConfig;
import org.com.ashen.service.dao.SaasAppSessionDao;
import org.com.ashen.service.dao.SaasSessionDao;

public class DataSourceChange {
	public static void setDataSource(SaasAppSessionDao saasAppSessionDao, String sessionid, JdbcConfig jdbcConfig) {
		 SaasAppSession saasAppSession = null;
			if(saasAppSessionDao.getAppSession(sessionid) == null){
				throw new BusinessException(ErrorEnum.SESSIONID_ERROR);
			}else {
				saasAppSession = saasAppSessionDao.getAppSession(sessionid);
			}
			DataSourceBeanBuilder builder = new DataSourceBeanBuilder(
	                "RTSaas"+saasAppSession.getDbid(),
	                jdbcConfig.dbUrl,
	                jdbcConfig.dbPort,
	                "RTSaas"+saasAppSession.getDbid(),
	                jdbcConfig.dbUser,
	                jdbcConfig.dbPass);
	        DataSourceContext.setDataSource(builder);
	}
	
	
	public static void setDataSource2(SaasSessionDao saasSessionDao, String sessionid, JdbcConfig jdbcConfig) {
		 SaasSession saasSession = null;
			if(saasSessionDao.getSession(Integer.parseInt(sessionid)) == null){
				throw new BusinessException(ErrorEnum.SESSIONID_ERROR);
			}else {
				saasSession = saasSessionDao.getSession(Integer.parseInt(sessionid));
			}
			DataSourceBeanBuilder builder = new DataSourceBeanBuilder(
	                "ASSaas"+saasSession.getDbid(),
	                jdbcConfig.dbUrl,
	                jdbcConfig.dbPort,
	                "ASSaas"+saasSession.getDbid(),
	                jdbcConfig.dbUser,
	                jdbcConfig.dbPass);
	        DataSourceContext.setDataSource(builder);
	}
	
	
	public static void clearDataSource() {
	        DataSourceContext.clearDataSource();
	}
}
